using Game.Core;
using Game.Main.Model;
using Game.Manager;
using UnityEngine;

namespace Game.Component
{
	public class KiddieRideItem : BaseItem
	{
		public enum State
		{
			Static = 0,
			Shake = 1
		}

		private float UP_Y = 0.1f;

		private State state;

		private Transform sitPoint;

		private Transform spring;

		private float sitPointY;

		private float frontY;

		private float backY;

		private float curY;

		private bool isUp = true;

		private bool isClickShake;

		private float clickShakeNum;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			sitPoint = model.Find("SitPoint");
			spring = model.Find("Spring");
			sitPointY = sitPoint.localPosition.y;
			frontY = frontMask.localPosition.y;
			backY = backMask.localPosition.y;
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (state == State.Shake)
			{
				if (isUp)
				{
					curY += Time.deltaTime / 4f;
					if (curY >= UP_Y)
					{
						curY = UP_Y;
						isUp = false;
					}
				}
				else
				{
					curY -= Time.deltaTime / 4f;
					if (curY <= 0f)
					{
						curY = 0f;
						isUp = true;
						if (isClickShake)
						{
							clickShakeNum += 1f;
							if (clickShakeNum >= 2f)
							{
								state = State.Static;
								RemoveAudioPlayer();
							}
						}
					}
				}
				SetKiddieRideY(curY);
				SetSpringScaleY(1f + 0.2f * (curY / UP_Y));
			}
			else if (curY != 0f)
			{
				curY -= Time.deltaTime / 4f;
				if (curY <= 0f)
				{
					curY = 0f;
				}
				SetKiddieRideY(curY);
				SetSpringScaleY(1f + 0.2f * (curY / UP_Y));
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (childItems.Count < 1)
			{
				state = State.Shake;
				isClickShake = true;
				clickShakeNum = 0f;
				Singleton<AudioManager>.Instance.PlayAudio("sound_kiddie_ride", true, GetAudioPlayer());
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool flag = base.AttachItem(item, depthType, attachType, updatePosition);
			if (flag)
			{
				CheckState();
			}
			return flag;
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			base.ReleaseAttachItem(item);
			CheckState();
		}

		public override void ResetState()
		{
			CheckState();
		}

		private void CheckState()
		{
			if (childItems.Count > 0)
			{
				SetState(State.Shake);
			}
			else
			{
				SetState(State.Static);
			}
		}

		private void SetState(State val)
		{
			state = val;
			if (state == State.Shake)
			{
				isClickShake = false;
				Singleton<AudioManager>.Instance.PlayAudio("sound_kiddie_ride", true, GetAudioPlayer());
				curY = 0f;
				isUp = true;
				SetSpringScaleY(1f);
				SetKiddieRideY(curY);
			}
			else
			{
				RemoveAudioPlayer();
			}
		}

		private void SetKiddieRideY(float y)
		{
			sitPoint.localPosition = new Vector3(sitPoint.localPosition.x, sitPointY + y, sitPoint.localPosition.z);
			frontMask.localPosition = new Vector3(frontMask.localPosition.x, frontY + y, frontMask.localPosition.z);
			backMask.localPosition = new Vector3(backMask.localPosition.x, backY + y, backMask.localPosition.z);
		}

		private void SetSpringScaleY(float y)
		{
			spring.localScale = new Vector3(1f, y, 1f);
		}
	}
}
